from api_test.api.feishu_api import FeiShuApi


class DeptApi(FeiShuApi):
    def __init__(self):
        super().__init__()
        self.dept_ids = []

    # 创建部门
    # def create(self, user_id_type="测试部门标题", description="测试部门描述", permissions="private", color=-1, user_id_type_alias=None):
    def create(self):
        path = "/open-apis/contact/v3/users"
        # data = {
        #     "user_id_type": user_id_type,
        #     "description": description,
        #     "permissions": permissions,
        #     "color": color,
        #     "user_id_type_alias": user_id_type_alias
        # }

        data = {
            "user_id": "ou_7dab8a3d3cdcc9da365777c7ad535d62",
            "name": "张三",
            "en_name": "San Zhang",
            "email": "zhangsan@gmail.com",
            "mobile": "13011111111",
            "mobile_visible": 'false',
            "gender": 1,
            "avatar_key": "2500c7a9-5fff-4d9a-a2de-3d59614ae28g",
            "department_ids": [
                "dept001"
            ],
            "leader_user_id": "ou_7dab8a3d3cdcc9da365777c7ad535d62",
            "city": "杭州",
            "country": "中国",
            "work_station": "北楼-H34",
            "join_time": 2147483647,
            "employee_no": "1",
            "employee_type": 1,
            "orders": [
                {
                    "department_id": "od-dept001",
                    "user_order": 100,
                    "department_order": 100
                }
            ],
            "custom_attrs": [
                {
                    "type": "TEXT",
                    "id": "DemoId",
                    "value": {
                        "text": "DemoText",
                        "url": "http://www.feishu.cn",
                        "pc_url": "http://www.feishu.cn",
                        "option_id": "edcvfrtg",
                        "generic_user": {
                            "id": "9b2fabg5",
                            "type": 1
                        }
                    }
                }
            ],
            "enterprise_email": "demo@mail.com",
            "job_title": "xxxxx",
            "need_send_notification": 'false',
            "notification_option": {
                "channels": [
                    "sms"
                ],
                "language": "zh-CN"
            }
        }
        # 发创建请求并将返回结果转换为json格式
        res = self.send_request("POST", self.base_url + path, json=data)
        print(res.text)
        # 判断是否创建成功， 成功则记录user_id
        # try:
        #     self.json_data = res.json()
        #     if res.status_code == 200:
        #         if self.jsonpath(expr="$.code")[0] == 0:
        #             user_id = self.jsonpath(expr="$..user_id")[0]
        #             self.user_ids.append(user_id)
        #             # print(self.user_ids)
        #             return self.json_data
        #     else:
        #         # print(f"部门创建失败：{res.status_code} {res.text}")
        #         return self.json_data
        # except Exception as E:
        #     print(res.status_code, res.text)

    # 根据 user_id 删除部门
    def delete(self, user_id):
        path = f"/open-apis/contact/v3/users/{user_id}"
        res = self.send_request("DELETE", self.base_url + path)
        # print(res)
        return res.json()

    def deletes(self, *args):
        # print(args)
        if args[0] == "all":
            # 返回的结果保存在self.json_data
            self.list()
            # self.jsonpath()默认读取self.json_data
            print(self.json_data)
            self.user_ids = self.jsonpath("$..user_id")
            # print(self.user_ids)
            for user_id in self.user_ids:
                self.delete(user_id)
        for user_id in args:
            self.delete(user_id)
        self.list()
        if len(self.jsonpath("$..user_id")) > 1:
            return False
        return True

    # 获取部门
    def get(self):
        # path = f"/open-apis/contact/v3/departments"
        path = f"/open-apis/contact/v3/users/b6f34d29"
        res = self.send_request("GET", self.base_url + path)
        return res.json()

    # 获取部门列表
    def list(self, page_size=100, page_token='', sync_token=''):
    # def list(self):
        path = "/open-apis/contact/v3/users"
        data = {
            "page_size": page_size,
            "page_token": page_token,
            "sync_token": sync_token
        }
        self.json_data = self.send_request("GET", self.base_url + path, json=data).json()
        # print(res.json())
        return self.json_data

    # 根据 user_id 更新部门
    def update(self, user_id, user_id_type, description, permissions, color, user_id_type_alias):
        path = f"/open-apis/contact/v3/users/{user_id}"
        data = {
            "user_id_type": user_id_type,
            "description": description,
            "permissions": permissions,
            "color": color,
            "user_id_type_alias": user_id_type_alias
        }
        res = self.send_request("PATCH", self.base_url + path, json=data)
        return res.json()

    # 搜索部门
    def search_department(self, query=None):
        path = "/open-apis/contact/v3/departments/search"
        payload = {
            "query": query,
        }

        res = self.send_request("POST", self.base_url + path, json=payload)
        # 判断请求结果
        if res.status_code == 200:
            self.json_data = res.json()
            if self.jsonpath(expr="$.code")[0] == 200:
                self.dept_ids += self.jsonpath("$..id")
                print(f"\n检查结果: {self.dept_ids}")
        else:
            print(res.status_code, res.text)

    # 订阅部门
    def subscribe(self, user_id):
        path = f"/open-apis/contact/v3/users/{user_id}/unsubscribe"
        res = self.send_request("POST", self.base_url + path)
        return res.json()

    # 取消订阅部门
    def unsubscribe(self, user_id):
        path = f"/open-apis/contact/v3/users/{user_id}/subscribe"
        res = self.send_request("POST", self.base_url + path)
        return res.json()

    # 订阅部门变更事件
    def subscription(self):
        path = "/open-apis/contact/v3/users/subscription"
        res = self.send_request("POST", self.base_url + path)
        return res.json()



